<template>
	<view class="">
		<Header title="收货地址" :showBack="true"></Header>
		<view class="form-box">
			<u--form labelPosition="left" :model="model1" :rules="rules" ref="uForm" labelWidth="auto" labelAlign="right" errorType="toast">
				<u-form-item label="省份" prop="userInfo.province" borderBottom ref="item1">
					<u--input v-model="model1.userInfo.province" border="none" placeholder="请填写省份"></u--input>
				</u-form-item>
				<u-form-item label="城市" prop="userInfo.city" borderBottom ref="item1">
					<u--input v-model="model1.userInfo.city" border="none" placeholder="请填写城市"></u--input>
				</u-form-item>
				<u-form-item label="地区" prop="userInfo.area" borderBottom ref="item1">
					<u--input v-model="model1.userInfo.area" border="none" placeholder="请填写区域"></u--input>
				</u-form-item>
				<u-form-item label="详细地址" prop="userInfo.address" borderBottom ref="item1">
					<u--input v-model="model1.userInfo.address" border="none" placeholder="请填写详细地址"></u--input>
				</u-form-item>
			</u--form>
			<view style="height: 160upx;"></view>
			<u-button type="primary" @click="submit" style="background-color: #2BAC6B;" color="#2BAC6B">提交收货地址</u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				info:uni.getStorageSync('info'),
				appId:getApp().globalData.appId,
				showSex: false,
				model1: {
					userInfo: {
						province: '',
						city: '',
						area: '',
						address: ''
					},
				},
				replacementOrderNo: '',
				rules: {
					'userInfo.province': {
						type: 'string',
						required: true,
						message: '请填写省份',
						trigger: ['blur', 'change']
					},
					'userInfo.city': {
						type: 'string',
						required: true,
						message: '请填写城市',
						trigger: ['blur', 'change']
					},
					'userInfo.area': {
						type: 'string',
						required: true,
						message: '请填写区域',
						trigger: ['blur', 'change']
					},
					'userInfo.address': {
						type: 'string',
						required: true,
						message: '请填写详细地址',
						trigger: ['blur', 'change']
					},
				},
			};
		},
		methods: {
			showTost(msg){
				uni.showToast({
					icon: 'none',
					title: msg
				})
			},
			createOrder(){
				let that = this;
				this.$http('/minprogram/api/member/replacement',{
					  "paymentType":1,
						"payType":12,
						"cardNo":this.info.cardNo,
						"openId":uni.getStorageSync('openId'),
						"appid": this.appId,
						"replacementAmount": 2,
						"address": this.model1.userInfo.address,
						"province":this.model1.userInfo.province,
						"city":this.model1.userInfo.city,
						"area":this.model1.userInfo.area,
						"replacementOrderNo": this.replacementOrderNo,
				},res=>{
					this.$http('/pay/api/pay/pay',res.data,pay=>{
						uni.requestPayment({
						    provider: 'wxpay',
						    timeStamp: pay.data.timeStamp,
						    nonceStr: pay.data.nonceStr,
						    package: pay.data.package,
						    signType: pay.data.signType,
						    paySign: pay.data.paySign,
						    success: function (res1) {
									uni.showToast({
										title: '申请成功！',
										icon: 'none'
									})
									setTimeout(()=>{
										uni.switchTab({
											url: '/pages/index/index'
										})
									},2000)
						    },
						    fail: function (err) {
						    }
						});
					})
					
				})
			},
			saveDeliveryAddress(){
				uni.showLoading({
					mask: true,
					title: '提交中'
				})
				let data = {
							"replacementOrderNo": this.replacementOrderNo,
							"address": this.model1.userInfo.address,
							"province":this.model1.userInfo.province,
							"city":this.model1.userInfo.city,
							"area":this.model1.userInfo.area
						}
				this.$http('/common/api/k12/saveReplacementAddress',data,res=>{
					uni.hideLoading()
					if(JSON.stringify(res.data) == 'true'){
						uni.showToast({
							title: '提交成功！',
							success: (res) => {
								setTimeout(()=>{
									this.createOrder()
								},1500)
							}
						})
					}else{
						uni.showToast({
							title: JSON.stringify(res.error.message),
							success: () => {
								uni.reLaunch({
									url: '/pages/index/index'
								})
							}
						})
					}
				},'POST',err=>{
					uni.hideLoading()
					uni.showToast({
						title: JSON.stringify(err.error.message),
						success: () => {
							uni.reLaunch({
								url: '/pages/index/index'
							})
						}
					})
				})
			},
			submit(){
				if(!this.model1.userInfo.address) return this.showTost('请填写详细地址');
				if(!this.model1.userInfo.province) return this.showTost('请填写省份');
				if(!this.model1.userInfo.city) return this.showTost('请填写城市');
				if(!this.model1.userInfo.area) return this.showTost('请填写区域');
				this.createOrder()
			},
		},
		onLoad(e) {
			if(e.replacementOrderNo){
				this.replacementOrderNo = e.replacementOrderNo
			}
		}
	};
</script>

<style lang="less">
	.form-box{
		margin: 20upx;
		font-size: 30upx;
		background: #fff;
		padding: 20rpx;
		border-radius: 14rpx;
		box-shadow: 0 0 10rpx 10rpx rgba(0, 0, 0, .04);
	}
	
</style>